package com.jackyzgy.test.thread;

import java.util.List;
import java.util.Vector;

public class ThreadWaitAndOthers {
	
	static List list = new Vector();
	private long allcount = 4;
	static boolean groupRunning = true;
	
	public static void main(String[] args) {
		ThreadWaitAndOthers threadWaitAndOthers = new ThreadWaitAndOthers();
		
		ThreadGroup group = new ThreadGroup("myGroup");
		Thread myThread1 = new Thread(group,new MyThread1(threadWaitAndOthers),"myThread1");
		Thread myThread2 = new Thread(group,new MyThread2(threadWaitAndOthers),"myThread2");
		
		myThread1.start();
		myThread2.start();
		
	}
	
	public synchronized void putObject(Object object){
		while(list.size() == allcount){
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		list.add(object);
		System.out.println("put a object");
		notifyAll();
	}
	
	public synchronized void getObject(){
		while(list.size() == 0){
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		for (int i = 0; i < list.size(); i++) {
			System.out.println("get a object");
			list.remove(i);
		}
		notifyAll();
	}
}

class MyThread1 extends Thread {
	
	ThreadWaitAndOthers threadWaitAndOthers = null;
	
	public MyThread1(ThreadWaitAndOthers threadWaitAndOthers) {
		this.threadWaitAndOthers = threadWaitAndOthers;
	}
	
	@Override
	public void run() {
		while(true){
			threadWaitAndOthers.putObject(new Object());
		}
	}
	
}

class MyThread2 extends Thread {
	
	ThreadWaitAndOthers threadWaitAndOthers = null;
	
	public MyThread2(ThreadWaitAndOthers threadWaitAndOthers) {
		this.threadWaitAndOthers = threadWaitAndOthers;
	}

	@Override
	public void run() {
		while(true){
			threadWaitAndOthers.getObject();
		}
	}
	
}

class TimerTaskTest extends java.util.TimerTask{  
    
    public void run() {
       System.out.println("start");  
    }  
} 